<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="ruleForm" :model="form" :rules="rules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="上级分类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pid">
              <a-select allowClear v-model="form.pid" @change="changePid" style="width: 100%">
                <!-- <a-select-option :key="''" :value="null">
                  不选择
                </a-select-option> -->
                <a-select-option v-for="(item,index) in treeData" :key="index" :value="item.id">
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="分类名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="form.name" placeholder="请输入分类名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="!form.pid">
            <a-form-model-item label="图标链接" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="imgUrl">
              <j-image-upload class="avatar-uploader" :accept="'image/png,image/jpeg'" text="上传" v-model="form.imgUrl"></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="form.pid">
            <a-form-model-item label="分类banner(500*160)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="imgUrl">
              <j-image-upload class="avatar-uploader" :accept="'image/png,image/jpeg'" text="上传500*160" v-model="form.imgUrl"></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sequence">
              <a-input-number :min="1" v-model="form.sequence" placeholder="请输入排序" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否显示" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isShow">
              <j-dict-select-tag v-model="form.isShow"  dictCode="yn" />
            </a-form-model-item>
          </a-col>

        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'
import JFormContainer from '@/components/jeecg/JFormContainer'
import JDate from '@/components/jeecg/JDate'
import JImageUpload from '@/components/jeecg/JImageUpload'
import JDictSelectTag from '@/components/dict/JDictSelectTag'

export default {
  name: 'SmsCategoryForm',
  components: {
    JFormContainer,
    JDate,
    JImageUpload,
    JDictSelectTag
  },
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => { },
      required: false
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    },
    treeData: {
      type: Array,
      default: [],
      required: false
    },
  },
  data () {
    return {
      form: {

        name: '',
        imgUrl: '',
        pid: null,
        sequence: 1,
        isShow: 1
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      rules: {
        name: [
          { required: true, message: '请输入分类名称!' },
        ],
        // imgUrl: [
        //   { required: true, message: '请输入图标链接!' },
        // ],
      },
      url: {
        add: "/mallBase/smsCategory/add",
        edit: "/mallBase/smsCategory/edit",
        queryById: "/mallBase/smsCategory/queryById"
      }
    }
  },
  computed: {
    formDisabled () {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton () {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    }
  },
  created () {
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData()
  },
  methods: {
    add () {
      let sequence = 1
      if (this.treeData.length > 0) {
        sequence = this.treeData[this.treeData.length - 1].sequence + 1
      }
      this.edit({
        name: '',
        imgUrl: '',
        pid: null,
        sequence: sequence,
        isShow: 1
      })

    },
    edit (record) {
      this.form = Object.assign({}, record)
      this.visible = true
    },
    changePid (e) {
      console.log(e)
      if (e && this.treeData.length > 0) {
        for (let i = 0; i < this.treeData.length; i++) {
          if (this.treeData[i].id == e) {
            if (this.treeData[i].children && this.treeData[i].children.length > 0) {
              this.form.sequence = this.treeData[i].children[this.treeData[i].children.length - 1].sequence + 1
            }
          }
        }
      }

    },
    //渲染流程表单数据
    showFlowData () {
      if (this.formBpm === true) {
        let params = { id: this.formData.dataId }
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result)
          }
        })
      }
    },
    submitForm () {
      const that = this
      // 触发表单验证
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.form.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign({}, this.form)
          console.log("表单提交数据", formData)
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    },
    popupCallback (row) {
      this.form = row
    },
  }
}
</script>
